package org.linys.action.dict;


import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.dict.City;
import org.linys.model.system.Operator;
import org.linys.service.dict.CityServ;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({  "unchecked" })
@Controller("CityAction")
@Scope("prototype")
public class CityAction extends BaseAction implements ModelDriven{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private City city = new City();
	private CityServ cityServ ;
	private ResponseMsgUtil responseMsgUtil;
	private PageUtil pageUtil;
	private String cityIDsStr;
	
	/**
	 * 加载地域城市树的根节点
	 */
	public void selectTreeRoot() {
		try{
		City root = cityServ.selectTreeRoot();
		if(root.getChildrenCount()>0){
			List<City> citys=cityServ.selectTreeNode(root);
			for(City city:citys){
				if(city.getChildrenCount()>0){
					city.setState("closed");
				}
			}
			root.setChildrenCitys(citys);
		}
		this.responseMsgUtil.doResponse("["+root.toString()+"]");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 
	 */
	public void selectTreeNode() {
		try{
				List<City> citys=cityServ.selectTreeNode(city);
				for(City city:citys){
					if(city.getChildrenCount()>0){
						city.setState("closed");
					}
				}
				this.responseMsgUtil.doResponse(citys.toString());
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	
	/**
	 * 单击树节点，属于该节点下的子节点信息
	 */
	public void getTreeNodeTabel() {
			
			
		try{
			String rows = request.getParameter("rows");
			String page = request.getParameter("page");
			
			
			this.pageUtil.cal(cityServ.count(city), rows, page);

			List<City> citys = cityServ.getTreeNodeTabel(city, this.pageUtil.getStart(), this.pageUtil.getRows());
			JSONArray jsonArray = JSONArray.fromObject(citys);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加地域城市
	 */
	public void add() {
			
			
	try{
		Operator operator=(Operator)request.getSession().getAttribute("operator_session");
		city.setHandlerID(operator.getOperatorID_());
		city.setOperatorID(operator.getOperatorID_());
		if (cityServ.save(city) > 0) {
			JSONObject object = new JSONObject();
			object.put("success", 1);
			object.put("cityID", city.getCityID());
			this.responseMsgUtil.doResponse(object.toString());
		}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 添加地域城市验证
	 */
	public void insertValdate() {
		JSONObject object = new JSONObject();
		try{
			int result=cityServ.insertValdate(city) ;
			object.put("success", 1);
			object.put("cityTypeID", result);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 批量删除
	 */
	public void mulDelete() {
		JSONObject object = new JSONObject();
		try{
			String [] cityIDArray=this.cityIDsStr.split(",");
			for(String cityID:cityIDArray){
				cityServ.deleteByCityID(new Integer(cityID));
			}
			
			object.put("success", 1);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	
	/**
	 * 选择地域城市
	 */
	public void choose() {
		try{
			
			this.pageUtil.cal(cityServ.countChoose(city), city.getRows(), city.getPage());

			List<City> citys = cityServ.viewChoose(city, this.pageUtil.getStart(), this.pageUtil.getRows());
			JSONArray jsonArray = JSONArray.fromObject(citys);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 修改地域城市
	 */
	public void update() {
			
			
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			city.setOperatorID(currentOperator.getOperatorID_());
			if (cityServ.update(city) > 0) {
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	

	public CityServ getCityServ() {
		return cityServ;
	}
	@Autowired
	public void setCityServ(CityServ cityServ) {
		this.cityServ = cityServ;
	}

	public ResponseMsgUtil getResponseMsgUtil() {
		return responseMsgUtil;
	}
	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}
	
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public Object getModel() {
		return city;
	}
	public String getCityIDsStr() {
		return cityIDsStr;
	}
	public void setCityIDsStr(String cityIDsStr) {
		this.cityIDsStr = cityIDsStr;
	}
	
}
